package com.mashibing.juc.c_020;

import java.util.concurrent.Exchanger;
import java.util.concurrent.TimeUnit;

public class T12_TestExchanger {

    static Exchanger<String> exchanger = new Exchanger<>();

    public static void main(String[] args) {

        new Thread(()->{
            String s = "T1";
            try {
//                TimeUnit.MILLISECONDS.sleep(6666);
//                s = exchanger.exchange(s);
                s = exchanger.exchange(s, 888, TimeUnit.MILLISECONDS);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " " + s);
        }, "t1").start();

        new Thread(()->{
            String s = "T3";
            try {
//                TimeUnit.MILLISECONDS.sleep(123);
                s = exchanger.exchange(s);
//                s = exchanger.exchange(s, 888, TimeUnit.MILLISECONDS);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " " + s);

        }, "t3").start();

        new Thread(()->{
            String s = "T2";
            try {
                s = exchanger.exchange(s);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " " + s);
        }, "t2").start();

        /*new Thread(()->{
            String s = "T3";
            try {
                TimeUnit.MILLISECONDS.sleep(123);
                s = exchanger.exchange(s, 88, TimeUnit.MILLISECONDS);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " " + s);

        }, "t3").start();

        new Thread(()->{
            String s = "T4";
            try {
//                TimeUnit.MILLISECONDS.sleep(123);
                s = exchanger.exchange(s, 88, TimeUnit.MILLISECONDS);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " " + s);

        }, "t4").start();*/

    }
}
